package com.welsy.user.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;	
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import mx.collections.ArrayCollection;
	import com.welsy.user.event.GetIterUserResultEvent;
	import com.welsy.user.business.GetIterUserResultDelegate;
	import com.welsy.iter.vo.iter.IterElementVO;	
	import com.welsy.user.view.scores.TestView;	
	import com.welsy.user.view.scores.CourseView;
	import com.welsy.authoring.vo.course.TestVO;		
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class GetIterUserResultCommand implements ICommand, IResponder
	{
	
		public function execute( event : CairngormEvent ): void
		{		
			var gic_eve : GetIterUserResultEvent = GetIterUserResultEvent(event);			
			var delegate : GetIterUserResultDelegate = new GetIterUserResultDelegate( this );
			delegate.getIterUserResult(gic_eve.userId, gic_eve.iterId);					    			
		}
	
		public function result( event : Object ) : void
		{				
			var umodel : UserModelLocator = UserModelLocator.getInstance();
			
			var xres : XMLList = new XMLList(event.result);
			trace(xres.toXMLString());
			
			var urNS : Namespace = new Namespace(NamespaceLocator.USER_RES_DATATYPES);
			var TAG_ELEMENTRESULTCOLLECTION : QName= new QName(urNS, "ElementResultCollection");
			var erc_t : XMLList = xres.descendants(TAG_ELEMENTRESULTCOLLECTION).children();
			
			var iNS : Namespace = new Namespace(NamespaceLocator.ITER_DATATYPES);
			var TAG_ELEMENT : QName = new QName(urNS, "Element");
			
			var TAG_ITERELEMENTID : QName = new QName(iNS, "IterElementId");
			var TAG_I_TITLE : QName = new QName(iNS, "Title");
			var TAG_DESCRIPTION : QName = new QName(iNS, "Description");
			var TAG_PARENTITERELEMENTID : QName = new QName(iNS, "ParentIterElementId");
			var TAG_COURSEID : QName = new QName(iNS, "CourseId");
			var TAG_FILEID : QName = new QName(iNS, "FileId");
			var TAG_COURSEONSITEID : QName = new QName(iNS, "CourseOnSiteId");
			var TAG_CREDITSNUMBER : QName = new QName(iNS, "CreditsNumber");
			var TAG_TYPE : QName = new QName(iNS, "Type");
			var TAG_POSITION : QName = new QName(iNS, "Position");
			var TAG_PRIORITY : QName = new QName(iNS, "Priority");
			
			var tNS : Namespace = new Namespace(NamespaceLocator.TEST_DATATYPES);
			var TAG_TESTRESULTCOLLECTION : QName = new QName(urNS, "TestResultCollection");
			var TAG_TESTRESULT : QName = new QName(urNS, "TestResult");
			var TAG_TEST : QName = new QName(urNS, "Test");
			var TAG_TESTID : QName = new QName(tNS, "TestID");
			var TAG_T_TITLE : QName = new QName(tNS, "Title");
			var TAG_QUESTIONSKIPTYPE : QName = new QName(tNS, "QuestionSkipType");
			var TAG_QUESTIONWEIGHTTYPE : QName = new QName(tNS, "QuestionWeightType");
			var TAG_PASSINGCONDITION : QName = new QName(tNS, "PassingCondition");
			var TAG_DISPLAYCORRECTED : QName = new QName(tNS, "DisplayCorrected");
			var TAG_DISPLAYFEEDBACK : QName = new QName(tNS, "DisplayFeedback");
			var TAG_DISPLAYCORRECTANSWER : QName = new QName(tNS, "DisplayCorrectAnswer");
			var TAG_TRACECONSULTATIONTIME : QName = new QName(tNS, "TraceConsultationTime");
			var TAG_CONSULTATIONTIMELIMIT : QName = new QName(tNS, "ConsultationTimeLimit");
			
			var TAG_PASSED : QName = new QName(urNS, "Passed");
			var TAG_SCORE : QName = new QName(urNS, "Score");
			
			var TAG_CREDITS : QName = new QName(urNS, "Credits");
			var TAG_TOTAL : QName = new QName(urNS, "Total");
			var TAG_ACHIEVED : QName = new QName(urNS, "Achieved");
			
			var result : ArrayCollection = new ArrayCollection;
			for each(var ert_t : XML in erc_t) {
				var ie : IterElementVO = new IterElementVO;
				var elem_t : XMLList = ert_t.child(TAG_ELEMENT);
				
				ie.iterElementId = elem_t.child(TAG_ITERELEMENTID);
				ie.title = elem_t.child(TAG_I_TITLE);
				ie.Description = elem_t.child(TAG_DESCRIPTION);
				ie.ParentIterElementId = elem_t.child(TAG_PARENTITERELEMENTID);
				ie.CourseId = elem_t.child(TAG_COURSEID);
				ie.fileId = elem_t.child(TAG_FILEID);
				ie.CourseOnSiteId = elem_t.child(TAG_COURSEONSITEID);
				ie.CreditsNumber = elem_t.child(TAG_CREDITSNUMBER);
				ie.type = elem_t.child(TAG_TYPE);
				ie.position = elem_t.child(TAG_POSITION);
				ie.Priority = elem_t.child(TAG_PRIORITY);
				
				var trc_t : XMLList = ert_t.descendants(TAG_TESTRESULTCOLLECTION).children();
				var testViews : ArrayCollection = new ArrayCollection;
				var place : int = 1;
				for each(var tr_t : XML in trc_t) {
					var tvo : TestVO = new TestVO;
					var test_t : XMLList = tr_t.child(TAG_TEST);
					
					tvo.testId = test_t.child(TAG_TEST);
					tvo.title = test_t.child(TAG_T_TITLE);
					tvo.skipType = test_t.child(TAG_QUESTIONSKIPTYPE);
					tvo.questionWeightType = test_t.child(TAG_QUESTIONWEIGHTTYPE);
					tvo.passingCondition = test_t.child(TAG_PASSINGCONDITION);
					tvo.displayCorrected = test_t.child(TAG_DISPLAYCORRECTED);
					tvo.displayFeedback = test_t.child(TAG_DISPLAYFEEDBACK);
					tvo.displayCorrectAnswere = test_t.child(TAG_DISPLAYCORRECTANSWER);
					tvo.traceConsultationTime = test_t.child(TAG_TRACECONSULTATIONTIME);
					tvo.consultationTimeLimit = test_t.child(TAG_CONSULTATIONTIMELIMIT);
					
					var testView : TestView = new TestView;
					testView.place = place++;
					testView.test = tvo;
					testView.passed = (tr_t.child(TAG_PASSED) == "true") ? true : false;
					testView.score = tr_t.child(TAG_SCORE);
					
					testViews.addItem(testView);
				}
				var courseView : CourseView = new CourseView;
				courseView.course = ie;
				courseView.testViews = testViews;
				courseView.achieved = ert_t.child(TAG_CREDITS).child(TAG_ACHIEVED);
				courseView.available = ert_t.child(TAG_CREDITS).child(TAG_TOTAL);
				result.addItem(courseView);
			}
			umodel.coursevws = result;
		}
				
		public function fault( event : Object ) : void
		{
			Alert.show("GetIterUserResultCommand.fault()!" );
		}
	}

}